<template>
  <div>
    <el-card>
      <el-row>
        <el-col>
          <el-tag type="success" style="width: 100%">{{ cinemaBmhBatchName }}</el-tag>
        </el-col>
      </el-row>
      <el-row :gutter="20" style="margin-top: 20px;">
        <el-col :span="12">
          <el-input placeholder="请输入手机号" class="input-with-select" v-model="queryInfo.mobile" clearable @clear="getBmhBatchCouponList" style="width: 240px;"/>
          <el-select v-model="queryInfo.status" size="small" clearable placeholder="请选择状态" style="width: 150px;margin-left: 20px;">
            <el-option
                v-for="item in statusList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
            </el-option>
          </el-select>
        </el-col>
        <el-col :span="3">
          <el-button type="primary" size="small" @click="getBmhBatchCouponList">搜索</el-button>
        </el-col>

        <el-col :span="4">
          <el-button type="warning" @click="showEditDialog('')">添加</el-button>
        </el-col>

        <el-col :span="4">
          <el-button type="warning" @click="showBatchUploadDialog">批量上传</el-button>
        </el-col>
      </el-row>
      <el-table :data="bmhList" border stripe>
        <el-table-column label="封面">
          <template slot-scope="sc">
            <img
                :src="sc.row.couponPassPicUrl"
                slot="reference"
                width="80"
                height="120"
            />
          </template>
        </el-table-column>
        <el-table-column label="手机号" prop="mobile" style="width: 120px;"></el-table-column>
        <el-table-column label="验证码" prop="validPass" style="width: 120px;"></el-table-column>
        <el-table-column label="状态" style="width: 120px;">
          <template slot-scope="scope">
            <el-switch v-if="scope.row.status=='Y' || scope.row.status=='N'"  v-model="scope.row.status" active-color="#13ce66" inactive-color="#ff4949"
                        active-value="Y" inactive-value="N" @change="updateStatus(scope.row)"></el-switch>
            <el-link type="danger" v-if="scope.row.status=='U'">已使用</el-link>
          </template>
        </el-table-column>
        <el-table-column label="修改" style="width: 120px;">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditDialog(scope.row.id)"></el-button>
          </template>
        </el-table-column>
        <el-table-column label="操作" style="width: 120px;">
          <template slot-scope="scope">
            <el-button type="warning" size="small" v-if="scope.row.orderId!=null" @click="getMovieOrder(scope.row.id)">关联电影订单</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNo"
        :page-sizes="[5, 10, 20]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>

    <el-dialog title="修改券码" :visible.sync="editDialogVisible" width="50%" @close="editDialogClosed">
      <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="90px" class="demo-ruleForm">
        <el-form-item label="手机号" prop="name">
          <el-input v-model="editForm.mobile" style="width:160px;"></el-input>
        </el-form-item>
        <el-form-item label="验证码" prop="validPass">
          <el-input v-model="editForm.validPass" style="width: 280px"></el-input>
        </el-form-item>
        <el-form-item label="券码图片">
          <el-upload
              action="/houtai/uploadImg"
              list-type="picture-card"
              :drag="true"
              :on-preview="handlePictureCardPreview"
              :on-success="handleImgSuccess"
              :show-file-list="true"
              :file-list="uploadList"
              :on-remove="handleRemove"
              :limit="maxUploadNum">
            <i slot="default" class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="60%" height="60%" :src="dialogImageUrl" alt="">
          </el-dialog>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editBmhBatch">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog title="批量上传" :visible.sync="batchDialogVisible" width="50%" @close="editDialogClosed">
      <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="90px" class="demo-ruleForm">
        <el-form-item label="券码图片">
          <el-upload
              action="/houtai/uploadBatchImg"
              list-type="picture-card"
              :multiple="true"
              :drag="true"
              :on-preview="handlePictureCardPreview"
              :on-success="handleImgSuccess2"
              :show-file-list="true"
              :file-list="uploadList"
              :on-remove="handleRemove">
            <i slot="default" class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="60%" height="60%" :src="dialogImageUrl" alt="">
          </el-dialog>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="batchDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editBatchBmhBatch">确 定</el-button>
      </span>
    </el-dialog>
    <el-drawer title="我是标题" :visible.sync="drawer" :with-header="false" size="300">
      <el-card>
        <el-descriptions title="基本信息" class="margin-top" :column="5" border>
          <el-descriptions-item label="订单号">{{ order.id }}</el-descriptions-item>
          <el-descriptions-item label="手机号">{{ order.mobile }}</el-descriptions-item>
          <el-descriptions-item label="支付状态">{{ order.payStatusText }}</el-descriptions-item>
          <el-descriptions-item label="支付方式">{{ order.payMethodText }}</el-descriptions-item>
          <el-descriptions-item label="订单状态">{{ order.orderStatusText }}</el-descriptions-item>
        </el-descriptions>
        <br/>
        <el-descriptions title="订单信息" class="margin-top" :column="2" border>
          <el-descriptions-item label="惠后单价">{{ order.orderUnitPrice }}</el-descriptions-item>
          <el-descriptions-item label="惠后金额">{{ order.orderAmount }}</el-descriptions-item>
          <el-descriptions-item label="原始单价">{{ order.channelUnitPrice }}</el-descriptions-item>
          <el-descriptions-item label="原始金额">{{ order.channelAmount }}</el-descriptions-item>
          <el-descriptions-item label="影院"><el-tag>{{ order.cinemaName }}</el-tag></el-descriptions-item>
          <el-descriptions-item label="城市">{{ order.cityName }}</el-descriptions-item>
          <el-descriptions-item label="电影"><el-tag>{{ order.movieName }}</el-tag></el-descriptions-item>
          <el-descriptions-item label="地址">{{ order.cinemaAddress }}</el-descriptions-item>
          <el-descriptions-item label="排期"><el-tag type="danger">{{ order.playTimeText }}</el-tag></el-descriptions-item>
          <el-descriptions-item label="版本">{{ order.edition }}</el-descriptions-item>
          <el-descriptions-item label="座位">
            <el-tag size="small" type="success">{{ order.seats }}</el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="影厅">{{ order.hallName }}</el-descriptions-item>
        </el-descriptions>
      </el-card>
    </el-drawer>
  </div>
</template>

<script>
import Api from '@/api/Movie.js'
import Util from '@/libs/utils'
export default {
  data() {
    return {
      queryInfo: {
        pageNo: 1,
        pageSize: 20,
        mobile:'',
        status:'',
        cinemaBmhBatchId:null
      },
      cinemaBmhBatchName:'',
      bmhList: [],
      tm:[],
      total: 0,
      addDialogVisible: false,
      addForm: {},
      editDialogVisible: false,
      batchDialogVisible: false,
      dialogVisible: false,
      editForm: {
        cinemaBmhBatchId:null
      },
      editFormRules: {},
      dialogImageUrl: '',
      maxUploadNum:1,
      uploadList: [],
      btm:[],
      statusList: [{
        value: 'Y',
        label: '可用'
      },{
        value: 'N',
        label: '禁用'
      },{
        value: 'U',
        label: '已售出'
      }],
      order: {
        ticketImg : ''
      },
      drawer: false
    }
  },
  created() {
    this.queryInfo.cinemaBmhBatchId = this.$route.query.cinemaBmhBatchId;
    this.editForm.cinemaBmhBatchId = this.$route.query.cinemaBmhBatchId;
    this.getCinemaBmhBatch();
    this.getBmhBatchCouponList();
  },
  methods: {
    showBmhBatchCoupon() {
      this.editDialogVisible = true;
    },
    getCinemaBmhBatch() {
      Api.getBmhBatch({'id':this.queryInfo.cinemaBmhBatchId}).then(res => {
        if (res.success) {
          this.cinemaBmhBatchName = res.data.name
        } else {
          return this.$message.error('查询失败')
        }
      })
    },
    getBmhBatchCouponList() {
        Api.getBmhBatchCouponList(this.queryInfo).then(res => {
            if (res.success) {
                this.bmhList = res.data.records;
                this.total = res.data.total;
            } else {
                return this.$message.error('获取数据失败')
            }
        })
    },
    async showEditDialog(id) {
      Api.getBmhBatchCoupon({'id':id}).then(res => {
            if (res.success) {
              this.editForm = res.data
              this.editForm.cinemaBmhBatchId = this.$route.query.cinemaBmhBatchId;
              this.uploadList=[];
              if(this.editForm.couponPassPicUrl){
                this.uploadList.push({'url':this.editForm.couponPassPicUrl});
              }
              this.editDialogVisible = true
            } else {
              return this.$message.error('查询失败')
            }
        })
    },
    // 修改
    editBmhBatch() {
      this.$refs.editFormRef.validate(async (valid) => {
        if (!valid) {
          return
        }
        Api.saveBmhBatchCoupon(this.editForm).then(res => {
            if (res.success) {
              this.editDialogVisible = false
              this.getBmhBatchCouponList()
              this.$message.success('修改成功！')
            } else {
                return this.$message.error('修改失败: ' + res.msg)
            }
        })
      })
    },
    editBatchBmhBatch() {
      Api.saveBatchBmhBatchCoupon(this.editForm).then(res => {
        if (res.success) {
          this.batchDialogVisible = false
          this.getBmhBatchCouponList()
          this.$message.success('保存成功！')
        } else {
          return this.$message.error('修改失败: ' + res.msg)
        }
      })
    },
    updateStatus(obj) {
      Api.setBmhBatchCouponStatus({'id':obj.id, 'status':obj.status}).then(res => {
        if (res.success) {
          this.$message.success('更新状态成功！')
        } else {
          return this.$message.error('更新状态失败！')
        }
      })
    },
    getMovieOrder(id) {
      this.drawer = true;
      Api.getBmhRelateMovieOrder({'id': id}).then((res) => {
        if (res.success) {
          this.order = res.data;
          if(this.order.processUserName!=null){
            this.order_title = '订单详情（接单人：' + this.order.processUserName + ')';
          }else{
            this.order_title = '订单详情';
          }
          this.quantity = res.data.quantity;
          this.uploadList = res.data.ticketImgList;
          this.showUploadList();
        } else {
          return this.$message.error("获取订单失败");
        }
      });
    },
    editDialogClosed() {
      this.$refs.editFormRef.resetFields()
    },
    handleSizeChange(pageSize) {
      this.queryInfo.pageSize = pageSize
      this.getBmhBatchCouponList()
    },
    handleCurrentChange(pageNo) {
      this.queryInfo.pageNo = pageNo
      this.getBmhBatchCouponList()
    },
    addDialogClosed() {
      this.$refs.addFormRef.resetFields()
    },
    handleRemove(file, fileList) {
      console.log(file.url);
      var newUploadList = [];
      this.uploadList.forEach(function (v, index) {
        if(file.url!=v.url){
          newUploadList.push(v);
        }
      });
      this.uploadList = newUploadList;
      this.showUploadList();
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    handleImgSuccess(res, file) {
      this.uploadList.push(res.data);
      this.showUploadList();
    },
    handleImgSuccess2(res, file, fileList) {
      console.info("aaaa1:" + JSON.stringify(res.data));
      console.info("size:" + JSON.stringify(file));
      console.info("arrstr:" + JSON.stringify(fileList));
      var _self = this;
      fileList.forEach((item) => {
        if (item.response) {
          let obj = {};
          obj.name = item.response.data.name;
          obj.url = item.response.data.url;
          _self.uploadList.push(obj);
        }
      });
      console.info("sss2:" + this.uploadList.length);
      this.showUploadList();
    },
    showBatchUploadDialog(){
      this.btm = [];
      this.batchDialogVisible = true;
    },
    showUploadList(){
      var tm = [];
      this.uploadList.forEach(function (v, index) {
        console.warn(v.url,index);
        tm.push(v.url);
      });
      if(tm.length>0){
        this.editForm.couponPassPicUrl = tm.join(',');
      }else {
        this.editForm.couponPassPicUrl = '';
      }
    }
  },
}
</script>

<style lang="less" scoped>
</style>
